<template>
  <div class="position-manage">
    <!-- 搜索区域 -->
    <el-card class="search-card" shadow="hover">
      <el-form :model="searchForm" inline>
        <el-form-item label="岗位名称">
          <el-input
            v-model="searchForm.name"
            placeholder="请输入岗位名称"
            clearable
            class="search-input"
          />
        </el-form-item>
        <el-form-item label="岗位编码">
          <el-input
            v-model="searchForm.code"
            placeholder="请输入岗位编码"
            clearable
            class="search-input"
          />
        </el-form-item>
        <el-form-item label="状态">
          <el-select
            v-model="searchForm.status"
            placeholder="请选择状态"
            clearable
            class="search-select"
          >
            <el-option label="正常" value="normal" />
            <el-option label="停用" value="disabled" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">
            <el-icon><Search /></el-icon>搜索
          </el-button>
          <el-button @click="handleReset">
            <el-icon><RefreshLeft /></el-icon>重置
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 工具栏 -->
    <div class="toolbar">
      <div class="left">
        <el-button type="primary" @click="handleAdd">
          <el-icon><Plus /></el-icon>新增岗位
        </el-button>
        <el-button type="danger" @click="handleBatchDelete" :disabled="!selectedPositions.length">
          <el-icon><Delete /></el-icon>批量删除
        </el-button>
      </div>
      <div class="right">
        <el-button type="success" @click="handleExport">
          <el-icon><Download /></el-icon>导出
        </el-button>
      </div>
    </div>

    <!-- 岗位列表 -->
    <el-card shadow="hover">
      <el-table
        :data="positionList"
        border
        stripe
        v-loading="loading"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column type="index" label="序号" width="60" />
        <el-table-column prop="name" label="岗位名称" show-overflow-tooltip />
        <el-table-column prop="code" label="岗位编码" width="120" />
        <el-table-column prop="order" label="排序" width="80" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="row.status === 'normal' ? 'success' : 'danger'">
              {{ row.status === 'normal' ? '正常' : '停用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="180" />
        <el-table-column label="操作" width="180" fixed="right">
          <template #default="{ row }">
            <el-button-group>
              <el-tooltip content="编辑" placement="top">
                <el-button type="primary" link @click="handleEdit(row)">
                  <el-icon><Edit /></el-icon>
                </el-button>
              </el-tooltip>
              <el-tooltip content="删除" placement="top">
                <el-button type="danger" link @click="handleDelete(row)">
                  <el-icon><Delete /></el-icon>
                </el-button>
              </el-tooltip>
            </el-button-group>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination">
        <el-pagination
          v-model:current-page="page.current"
          v-model:page-size="page.size"
          :total="page.total"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 编辑对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="dialogType === 'add' ? '新增岗位' : '编辑岗位'"
      width="500px"
      destroy-on-close
    >
      <el-form
        ref="formRef"
        :model="formData"
        :rules="formRules"
        label-width="80px"
      >
        <el-form-item label="岗位名称" prop="name">
          <el-input v-model="formData.name" placeholder="请输入岗位名称" />
        </el-form-item>
        <el-form-item label="岗位编码" prop="code">
          <el-input v-model="formData.code" placeholder="请输入岗位编码" />
        </el-form-item>
        <el-form-item label="排序" prop="order">
          <el-input-number v-model="formData.order" :min="0" :max="999" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="formData.status">
            <el-radio label="normal">正常</el-radio>
            <el-radio label="disabled">停用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="formData.remark"
            type="textarea"
            placeholder="请输入备注"
            :rows="3"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSubmit">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import {
  Search,
  RefreshLeft,
  Plus,
  Edit,
  Delete,
  Download
} from '@element-plus/icons-vue'

// 岗位项接口
interface PositionItem {
  id: string | number
  name: string
  code: string
  order: number
  status: 'normal' | 'disabled'
  remark?: string
  createTime: string
}

// 搜索表单
const searchForm = ref({
  name: '',
  code: '',
  status: ''
})

// 分页配置
const page = ref({
  current: 1,
  size: 10,
  total: 0
})

// 状态定义
const loading = ref(false)
const dialogVisible = ref(false)
const dialogType = ref<'add' | 'edit'>('add')
const formRef = ref()
const positionList = ref<PositionItem[]>([])
const selectedPositions = ref<PositionItem[]>([])

// 表单数据
const formData = ref({
  id: '',
  name: '',
  code: '',
  order: 0,
  status: 'normal',
  remark: ''
})

// 表单校验规则
const formRules = {
  name: [
    { required: true, message: '请输入岗位名称', trigger: 'blur' },
    { min: 2, max: 50, message: '长度在 2 到 50 个字符', trigger: 'blur' }
  ],
  code: [
    { required: true, message: '请输入岗位编码', trigger: 'blur' },
    { pattern: /^[A-Z_]{2,50}$/, message: '只能包含大写字母和下划线', trigger: 'blur' }
  ],
  order: [
    { required: true, message: '请输入排序号', trigger: 'blur' }
  ],
  status: [
    { required: true, message: '请选择状态', trigger: 'change' }
  ]
}

// 处理搜索
const handleSearch = () => {
  page.value.current = 1
  fetchPositionList()
}

// 处理重置
const handleReset = () => {
  searchForm.value = {
    name: '',
    code: '',
    status: ''
  }
  handleSearch()
}

// 处理新增
const handleAdd = () => {
  dialogType.value = 'add'
  formData.value = {
    id: '',
    name: '',
    code: '',
    order: 0,
    status: 'normal',
    remark: ''
  }
  dialogVisible.value = true
}

// 处理编辑
const handleEdit = (row: PositionItem) => {
  dialogType.value = 'edit'
  formData.value = { ...row }
  dialogVisible.value = true
}

// 处理删除
const handleDelete = (row: PositionItem) => {
  ElMessageBox.confirm(
    '确定要删除该岗位吗？',
    '警告',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(() => {
    // 这里调用删除接口
    ElMessage.success('删除成功')
  })
}

// 处理批量删除
const handleBatchDelete = () => {
  if (selectedPositions.value.length === 0) {
    ElMessage.warning('请选择要删除的岗位')
    return
  }
  ElMessageBox.confirm(
    `确定要删除选中的 ${selectedPositions.value.length} 个岗位吗？`,
    '警告',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(() => {
    // 这里调用批量删除接口
    ElMessage.success('批量删除成功')
  })
}

// 处理导出
const handleExport = () => {
  // 这里调用导出接口
  ElMessage.success('导出成功')
}

// 处理表单提交
const handleSubmit = async () => {
  if (!formRef.value) return
  await formRef.value.validate((valid: boolean) => {
    if (valid) {
      // 这里调用新增或编辑接口
      ElMessage.success(dialogType.value === 'add' ? '新增成功' : '编辑成功')
      dialogVisible.value = false
      fetchPositionList()
    }
  })
}

// 处理表格选择变化
const handleSelectionChange = (selection: PositionItem[]) => {
  selectedPositions.value = selection
}

// 处理分页大小变化
const handleSizeChange = (size: number) => {
  page.value.size = size
  fetchPositionList()
}

// 处理页码变化
const handleCurrentChange = (current: number) => {
  page.value.current = current
  fetchPositionList()
}

// 获取岗位列表
const fetchPositionList = () => {
  loading.value = true
  // 这里调用获取岗位列表接口
  setTimeout(() => {
    // 模拟数据
    positionList.value = [
      {
        id: 1,
        name: '总经理',
        code: 'GM',
        order: 1,
        status: 'normal',
        createTime: '2024-01-01 12:00:00'
      },
      {
        id: 2,
        name: '技术总监',
        code: 'CTO',
        order: 2,
        status: 'normal',
        createTime: '2024-01-01 12:00:00'
      },
      {
        id: 3,
        name: '产品经理',
        code: 'PM',
        order: 3,
        status: 'normal',
        createTime: '2024-01-01 12:00:00'
      },
      {
        id: 4,
        name: '高级工程师',
        code: 'SE',
        order: 4,
        status: 'normal',
        createTime: '2024-01-01 12:00:00'
      },
      {
        id: 5,
        name: '人力资源',
        code: 'HR',
        order: 5,
        status: 'disabled',
        createTime: '2024-01-01 12:00:00'
      }
    ]
    page.value.total = 100
    loading.value = false
  }, 500)
}

// 初始化
onMounted(() => {
  fetchPositionList()
})
</script>

<style lang="scss" scoped>
.position-manage {
  .search-card {
    margin-bottom: 16px;
    
    :deep(.el-card__body) {
      padding: 16px;
    }
    
    .search-input {
      width: 200px;
    }
    
    .search-select {
      width: 160px;
    }
  }
  
  .toolbar {
    margin-bottom: 16px;
    display: flex;
    justify-content: space-between;
    gap: 16px;
    
    .el-button {
      transition: all 0.3s ease;
      
      &:hover {
        transform: translateY(-2px);
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
      }
      
      .el-icon {
        margin-right: 4px;
      }
    }
  }
  
  .el-card {
    border-radius: 8px;
    transition: all 0.3s ease;
    
    &:hover {
      box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
    }
  }
  
  :deep(.el-table) {
    border-radius: 8px;
    overflow: hidden;
    
    .el-button-group {
      .el-button {
        padding: 4px 8px;
        
        .el-icon {
          font-size: 16px;
        }
        
        &:hover {
          .el-icon {
            transform: scale(1.2);
          }
        }
      }
    }
  }
  
  .pagination {
    margin-top: 16px;
    display: flex;
    justify-content: flex-end;
  }
  
  :deep(.el-tag) {
    border-radius: 4px;
    
    &.el-tag--success {
      background: var(--el-color-success-light-9);
    }
    
    &.el-tag--danger {
      background: var(--el-color-danger-light-9);
    }
  }
}
</style> 